require 'RMagick'
class ImagesController < ApplicationController
  
  # GET /images
  # GET /images.xml
  def index
    @images = Image.all
    @event = Event.all
    @image_status = ImageStatus.all
	
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @images }
    end
  end

  # GET /images/1
  # GET /images/1.xml
  def show
    @image = Image.find(params[:id])
    
    send_file @image.renderImage,
				  :filename => @image.picture, 
                                  :type => "image/#{@image.image_type}",
                                  :disposition => 'inline'
  end
  
  def thumbnail
    @image = Image.find(params[:id])
    
    send_file @image.renderThumbnail(params[:size]),
				  :filename => @image.picture,
                                  :type => "image/#{@image.image_type}",
                                  :disposition => 'inline'
  end

  # GET /images/new
  # GET /images/new.xml
  def new
    @image = Image.new
	@event = Event.all
	@thumbnail = Thumbnail.all
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @image }
    end
  end

  # GET /images/1/edit
  def edit
    @image = Image.find(params[:id])
    @event = Event.all
    @thumbnail = Thumbnail.all
  end

  # POST /images
  # POST /images.xml
  def create
    @image = Image.new(params[:image])
    
    respond_to do |format|
      if @image.save
        pic = params[:image][:picture]
	#Update @image.picture 
	@bienid=@image.id
	@image.title = pic.original_filename if @image.title.empty? || @image.title.nil?
	@image.picture =@bienid.to_s + "_" + pic.original_filename
	@image.width=ImageTools.getwidth(@file_path)
	@image.height = ImageTools.getheight(@file_path)
	#@file = pic
	#ImageTools.createsizeimage(@file)
	@image.save
	#Thumbnail image
	@ids = params[:ids]
	@ids.each do |object|
	@a = Thumbnail.find(object[0])
	if File.file?(@file_path)
            # create thumbnail
            ImageTools.createThumbnail(@file_path, "public/images/event/thumbnails/#{@image.id.to_s + "_" + @a.height + "x" + @a.width + "_" + pic.original_filename}", @a.height.to_i, @a.width.to_i)
			else
            @image.destroy
			end
		end
		# 
        format.html { redirect_to(@image, :notice => 'Image was successfully created.') }
        
        
        format.xml  { render :xml => @image, :status => :created, :location => @image }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @image.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /images/1
  # PUT /images/1.xml
  def update

    @image = Image.find(params[:id])

    respond_to do |format|
      if @image.update_attributes(params[:image])
        format.html { redirect_to(@image, :notice => 'Image was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @image.errors, :status => :unprocessable_entity }
      end
    end
  end
 
	

  # DELETE /images/1
  def destroy
	@image = Image.find(params[:id])
	@bienid = @image.id
	Dir.foreach("public/images/event/thumbnails"){|x| File.delete("public/images/event/thumbnails/"+x)  if x=~/^#{@bienid}(.*)/ }
	File.delete("public/images/event/"+@image.picture)
	@image.destroy
	respond_to do |format|
      format.html { redirect_to(articles_url) }
      format.xml  { head :ok }
    end
  end
  # DELETE /images/1.xml
  def destroy
    # DELETE Image record
    @image = Image.find(params[:id])
    @event = Event.find(params[:event_id])
    
    # DELETE image file
    @file_path = "public/images/event/#{@image.id.to_s + "_" + @image.picture}"
    @file_path_thumb = "public/images/event/thumbnails/#{@image.id.to_s + "_" + @image.picture}"
    File.delete(@file_path) if File.exist?(@file_path)
    File.delete(@file_path_thumb) if File.exist?(@file_path_thumb)
    
    @image.destroy
    
    respond_to do |format|
      format.html { redirect_to(images_url) }
      format.xml  { head :ok }
      format.js { render 'destroy.js.erb' }
    end
  end
  
  def upload
    @image = Image.new(params[:image])
    @event = Event.find(@image.event_id)           
    @image.event = @event
    
    pic = params[:image][:picture]
    @image.picture = pic.original_filename
    @image.title = pic.original_filename if @image.title.empty? || @image.title.nil?
    
 
    # FileUtils.copy(pic.local_path, "#{RAILS_ROOT}/public/images/#{pic.original_filename}")
    
    respond_to do |format|
      if @image.save          
          
          # Upload picture    
          @image.upload(pic)
          @image.createThumbnails
          
          format.html { render :layout => false }
         else
          format.html { render :layout => false }
      end
    end
    

  end
  
  def small_gallery
    render :layout => false
  end
  
  def related_images
    @event = Event.find(params[:eventid])
    @images = @event.images
    
    render :layout => false
  end
  
end
